﻿using System;
using System.Data;
using System.Web.UI;
using Solution.BusinessObject;

namespace Solution.CMS
{
    public partial class ChangePassword : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnChangePass_Click(object sender, EventArgs e)
        {
            var passwordEncrypt = CommonBO.EncryptMD5(Password.Text.Trim());
            var newPasswordEncrypt = CommonBO.EncryptMD5(NewPassword.Text.Trim());

            if (string.IsNullOrEmpty(passwordEncrypt))
            {
                return;
            }
            if (string.IsNullOrEmpty(NewPassword.Text.Trim()) || 
                string.IsNullOrEmpty(PasswordConfirm.Text.Trim()))
            {
                return;
            }

            if (NewPassword.Text.Trim() != PasswordConfirm.Text.Trim())
            {
                return;
            }

            var ds = new DataSet();
            ds.ReadXml(Server.MapPath("/CMS/users.xml"));

            if (ds.Tables.Count <= 0) return;
            if (ds.Tables[0].Rows[0]["username"].ToString() == Password.Text.Trim() &&
                ds.Tables[0].Rows[0]["password"].ToString() == passwordEncrypt)
            {
                ds.Tables[0].Rows[0]["password"] = newPasswordEncrypt;
                ds.WriteXml(Server.MapPath("/CMS/users.xml"));
                Response.Redirect("/CMS");
            }
        }
    }
}